package com.qbw.app.module.mine.fragment;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.qbw.actionmanager.ActionManager;
import com.qbw.app.R;
import com.qbw.app.action.UserInfoUpdateAction;
import com.qbw.app.databinding.MineFragmentBinding;
import com.qbw.app.model.UserInfo;
import com.qbw.app.module.common.fragment.CommonFragment;
import com.qbw.app.module.common.fragment.PhotoChooseDialogFragment;
import com.qbw.app.module.main.activity.MainActivity;
import com.qbw.app.module.mine.activity.MineSettingActivity;
import com.qbw.app.module.user.activity.UserAccountSafeActivity;
import com.qbw.app.module.vehicle.activity.VehicleElectricAreaActivity;
import com.qbw.app.module.vehicle.activity.VehicleManageActivity;
import com.qbw.app.module.vehicle.activity.VehicleNotificationVehicleListActivity;
import com.qbw.app.request.impl.UserRequest;

/**
 * @author QBW
 * @date 2019-09-22
 */
public class MineFragment extends CommonFragment<MineFragmentBinding> implements ActionManager.Listener {

    public static MineFragment createInstance() {
        MineFragment ins = new MineFragment();
        return ins;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mGeneralManager.getRequestManager().getUserRequest().getUserInfo(this).execute();
        mBinding.clVehicleManage.setOnClickListener(this);
        mBinding.tvEventWarning.setOnClickListener(this);
        mBinding.clElectricArea.setOnClickListener(this);
        mBinding.tvNotification.setOnClickListener(this);
        mBinding.clSetting.setOnClickListener(this);
        mBinding.tvNickname.setOnClickListener(this);
        mBinding.sdvVehicleAvatar.setOnClickListener(this);
        ActionManager.getInstance().addInterestedAction(this, UserInfoUpdateAction.class);
    }


    public MineFragmentBinding getBinding() {
        return mBinding;
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(UserRequest.METHOD_USERINFO)) {
            UserInfo.Response response = (UserInfo.Response) entity;
            UserInfo userInfo = response.getData();
            mBinding.setUser(userInfo);
        }
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_event_warning) {

        } else if (vid == R.id.cl_electric_area) {
            VehicleElectricAreaActivity.start(getActivity());
        } else if (vid == R.id.tv_notification) {
            VehicleNotificationVehicleListActivity.start(getActivity());
        } else if (vid == R.id.cl_setting) {
            MineSettingActivity.start(getActivity());
        } else if (vid == R.id.cl_vehicle_manage) {
            VehicleManageActivity.start(getActivity());
        } else if (vid == R.id.tv_nickname) {
            UserAccountSafeActivity.startActivity(getActivity(), mBinding.getUser());
        } else if (vid == R.id.sdv_vehicle_avatar) {
            Activity activity = getActivity();
            if (activity instanceof MainActivity) {
                if (((MainActivity) activity).isHavePhotoChoosePermission()) {
                    PhotoChooseDialogFragment.createInstance()
                                             .show(getChildFragmentManager(), "photo");
                }
            }
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onActionTriggered(Object action) {
        if (action instanceof UserInfoUpdateAction) {
            mGeneralManager.getRequestManager().getUserRequest().getUserInfo(this).execute();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ActionManager.getInstance().removeInterestedActions(this);
    }
}
